package com.hqd.ch03.v11.spel.standard;

import com.hqd.ch03.v11.spel.EvaluationContext;
import com.hqd.ch03.v11.spel.Expression;
import com.hqd.ch03.v11.spel.ast.SpelNodeImpl;
import com.hqd.ch03.v11.spel.context.StandardEvaluationContext;


/**
 * 单一的spel表达式
 */
public class SpelExpression implements Expression {
    private final String expression;

    /**
     * 根节点
     */
    private final SpelNodeImpl ast;
    private EvaluationContext evaluationContext = new StandardEvaluationContext();

    public SpelExpression(String expressionString, SpelNodeImpl ast) {
        this.expression = expressionString;
        this.ast = ast;
    }

    @Override
    public String getExpressionString() {
        return expression;
    }

    @Override
    public Object getValue() {
        return getValue(getEvaluationContext());
    }

    @Override
    public Object getValue(EvaluationContext context) {
        ExpressionState expressionState = new ExpressionState(context);
        return this.ast.getValue(expressionState);
    }

    @Override
    public void setValue(EvaluationContext context, Object value) {

    }

    public EvaluationContext getEvaluationContext() {
        return evaluationContext;
    }
}
